import { NotifyModel } from '@/models/notify'
import { getNotifyPage, NotifyVO, removeNotify } from '@/services/NotifyService'
import { useAccess, useModel } from '@@/exports'
import { ProColumns, ProTable } from '@ant-design/pro-components'
import { Button, Popconfirm, Space } from 'antd'

export interface NotifyProps {
  tableRef?: any
}

const NotifyTable = (props: NotifyProps) => {
    const access = useAccess()
  let { actions: notifyActions } = NotifyModel

  let { getDictEnum } = useModel('dict')

  /**
   * 列定义
   */
  const columns: ProColumns<NotifyVO>[] = [
    {
      title: 'id',
      dataIndex: 'id',
      width: 120,
      hideInTable: true,
      hideInSearch: true,
    },
    {
      title: '通知类型',
      dataIndex: 'notifyType',
      width: 120,
      valueEnum: getDictEnum('notify_type'),
      hideInSearch: true,
    },
    {
      title: 'webhook地址',
      dataIndex: 'webhookUrl',
      width: 400,
      ellipsis: true,
      hideInSearch: true,
    },
    {
      title: '备注',
      dataIndex: 'remark',
      hideInSearch: true,
    },
    {
      title: '操作',
      render: (_: any, record: NotifyVO) => [
        <Space key="operate">
          {access.hasNotifyUpdatePerm && (
            <Button type="link" size="small" onClick={() => notifyActions.openUpdateModal(true, record)}>
              编辑
            </Button>
          )}
          {access.hasNotifyUpdatePerm && (
            <Popconfirm
              title="确定要删除吗"
              onConfirm={() => {
                removeNotify(record.id).then(() => props.tableRef?.current?.resetFields())
              }}
              okText="确认"
              cancelText="取消"
            >
              <Button type="link" danger size="small">
                删除
              </Button>
            </Popconfirm>
          )}
        </Space>,
      ],
      width: 150,
      fixed: 'right',
      hideInSearch: true,
    },
  ]

  // 工具栏
  let actions = [
    access.hasNotifySavePerm && (
      <Button
        key="create"
        type="primary"
        onClick={() => {
          notifyActions.openUpdateModal(true, undefined)
        }}
      >
        添加
      </Button>
    ),
  ]

  return (
    <>
      <ProTable
        actionRef={props.tableRef}
        columns={columns}
        request={getNotifyPage}
        toolbar={{
          actions: actions,
        }}
        scroll={{ x: 500 }}
        sticky={{ offsetHeader: 55, offsetScroll: 5 }}
      ></ProTable>
    </>
  )
}

export default NotifyTable
